﻿using Microsoft.UI.Xaml.Data;

namespace DimensionsHelper.Client.WinUi.Helpers;

public partial class StringToBooleanConverter : IValueConverter
{
    public bool ReverseValue
    {
        get;
        set;
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        if (string.IsNullOrEmpty(value as string))
        {
            return ReverseValue;
        }

        if (bool.TryParse(value as string, out bool bln))
        {
            return ReverseValue ? !bln : bln;
        }

        return !ReverseValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language) =>
        value?.ToString() ?? "";
}